from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select

class Base():

    def __init__(self,driver:WebDriver):
        self.driver = driver

    def open_url(self,url):
        self.driver.get(url)

    def find_element(self,loc):
        try:
            WebDriverWait(self.driver,20,0.5).until(lambda driver:driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as e:
            print(e)

    def click(self,loc):
        self.find_element(loc).click()

    def send_keys(self,loc,info):
        self.find_element(loc).clear()
        self.find_element(loc).send_keys(info)

    def get_text(self,loc):
        return self.find_element(loc).text

    def scroll_x(self,x):
        self.driver.execute_script(f'window.scrollTo(0,{x})')
        # self.driver.execute_script(f'window.scrollTo(0,document.body.scrollHeight)')

    def iframe(self,loc):
        self.driver.switch_to.frame(self.find_element(loc))

    def iframe2(self):
        self.driver.switch_to.default_content()

    def xuan(self,loc):
        ActionChains(self.driver).move_to_element(self.find_element(loc)).perform()

    def select_value(self,loc,val):
        sel =Select(self.find_element(loc))
        sel.select_by_value(val)